﻿using System;
using System.Configuration;
using System.Reflection;
using Com.FastSearch.SharePoint.BL.SearchManager;
using Com.FastSearch.SharePoint.Interfaces;
using Com.FastSearch.Utils;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Com.FastSearch.SharePoint.UCF
{   
    /// <summary>
    /// ESPSearch part is the Controller of the MVC/UCF implementation the web parts are using.
    /// In a default implementation the static method InitDefaultSearchFactory can be used to register the default ESPSearchManagerFactory
    /// which uses the ESPSearchManager as search provider.
    /// 
    /// Once the search results are available (The search is only performed once per search group), the webparts <see cref="IESPSearchPartGUI.SetSearchResult"/> is called.
    /// </summary>
    public class ESPSearchPart
    {        
        /// <summary>
        /// Init the default IESPSearchManagerFactory used for searching.
        /// By default the <see cref="ESPSearchManagerFactory"/> is used as a factort.
        /// </summary>                
        public static void InitDefaultSearchFactory()
        {
            if (SimpleFactoryManager<IESPSearchManagerFactory>.Factory == null)
            {
                try
                {
                    string searchFactString = ConfigurationSettings.AppSettings["IESPSearchManagerFactory"];
                    string searchFactAssembly = ConfigurationSettings.AppSettings["IESPSearchManagerFactoryAssembly"];
                    if (!string.IsNullOrEmpty(searchFactString) && (!string.IsNullOrEmpty(searchFactAssembly)))
                    {
                        Assembly assem = Assembly.Load(searchFactAssembly);                       
                        Type type = assem.GetType(searchFactString);
                        IESPSearchManagerFactory searchFact = Activator.CreateInstance(type) as IESPSearchManagerFactory;
                        SimpleFactoryManager<IESPSearchManagerFactory>.Factory = searchFact;
                    }
                }
                catch (Exception) { }
                if (SimpleFactoryManager<IESPSearchManagerFactory>.Factory == null)
                {
                    SimpleFactoryManager<IESPSearchManagerFactory>.Factory = new ESPSearchManagerFactory();
                }
            }
        }
        
        IESPSearchResult _Result = null;

        /// <summary>
        /// Perform a search with the configured IESPSearchManager returned by the configured IESPSearchManagerFactory.
        /// The search is performed exactly once per search group (see ESPSearchWebPart for more information)
        /// If the search group has been configured to use a specific search connection use this one.<br></br>
        /// Otherwise the Sharepoint Web Application configured connection is used.
        /// </summary>
        /// <param name="searchRequest">The search parameters to be used for the search</param>
        /// <param name="GUI">The ESPSearchWebPart web part calling the search. Used to call the SetSearchResult method to pass the parameter to the web part</param>
        public void Search(IESPSearchSettings searchRequest, IESPSearchPartGUI GUI)
        {
            if (_Result == null)
            {
                // If no connection is provided use the Sharepoint WebApplication's default configuration
                if (searchRequest.ESPConnection == null)
                {
                    SPWebApplication webApp = SPContext.Current.Site.WebApplication;
                    // Get Search Manager to do the search
                    IESPSearchManager s = SimpleFactoryManager<IESPSearchManagerFactory>.Factory.Create(webApp);
                    _Result = s.Search(searchRequest,
                        System.Security.Principal.WindowsIdentity.GetCurrent());    
                } else
                {
                    IESPSearchManager s = SimpleFactoryManager<IESPSearchManagerFactory>.Factory.Create(searchRequest.ESPConnection);
                    _Result = s.Search(searchRequest,
                        System.Security.Principal.WindowsIdentity.GetCurrent());     
                }                
            }

            GUI.SetSearchResult(_Result);
        }
    }           
}
